package com.wt.admin.controller;


import com.wt.admin.entity.WqunOOS;
import com.wt.commonutils.R;
import com.wt.oos.Upload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Api(tags = "工具控制类")
@RestController
@RequestMapping("/util")
@CrossOrigin
public class UtilsController {


    @ApiOperation("项目删除")
    @PostMapping("/deleteUploadProject")
    @PreAuthorize("hasAnyAuthority('admin:util:deleteUploadProject')")
    public R  deleteUploadProject(@RequestBody WqunOOS wqunOOS) {
        boolean res = Upload.deleteUploadProject(wqunOOS.getOosImgPath());
        if(res){
            return R.ok().message("项目文件删除成功");
        }else{
            return R.error().message("项目文件删除失败");
        }
    }


    @ApiOperation("图片上传")
    @RequestMapping("/uploadImg")
    @PreAuthorize("hasAnyAuthority('util:uploadImg')")
    public R uploadImg(@RequestParam("imgFile") MultipartFile mFile) {
        try {
            ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String folder =System.getProperty("user.dir") +  "/upload/img";
        File imgFolder = new File(folder);// 文件夹
        String fname = mFile.getOriginalFilename();// 获取文件名
        String ext = "." + fname.substring(fname.lastIndexOf(".")+1);// 获取文件后缀
        if(!".jpg".equals(ext) && !".png".equals(ext)){
            return R.error().message("文件格式不正确");
        }
        String dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());// 获取时间字符串
        String newFileName = dateTimeFormatter + UUID.randomUUID().toString().replaceAll("-","") + ext;// 生成新的文件名
        File filePath = new File(imgFolder, newFileName);// 文件在本机的全路径
        if (!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
        }
        try{
            mFile.transferTo(filePath);
            // 返回文件名
            String resPath = Upload.UploadImg(filePath);
            filePath.delete();
            return R.ok().message("上传成功").data("resPath",resPath);
        }catch (IOException e){
            e.printStackTrace();
            return R.error().message("上传失败");
        }
    }


    @ApiOperation("图片删除")
    @PostMapping("/deleteUploadImg")
    @PreAuthorize("hasAnyAuthority('util:deleteUploadImg')")
    public R  deleteUploadImg(@RequestBody WqunOOS wqunOOS) {
        boolean res = Upload.deleteUploadImg(wqunOOS.getOosImgPath());
        if(res){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }


    @ApiOperation("文件上传")
    @RequestMapping("/uploadFile")

    public R uploadFile(@RequestParam("uploadFile") MultipartFile mFile) {
        try {
            ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String folder =System.getProperty("user.dir") +  "/upload/img";
        File imgFolder = new File(folder);// 文件夹
        String fname = mFile.getOriginalFilename();// 获取文件名
        String ext = "." + fname.substring(fname.lastIndexOf(".")+1);// 获取文件后缀
        String dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());// 获取时间字符串
        String newFileName = dateTimeFormatter + UUID.randomUUID().toString().replaceAll("-","") + ext;// 生成新的文件名
        File filePath = new File(imgFolder, newFileName);// 文件在本机的全路径
        if (!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
        }
        try{
            mFile.transferTo(filePath);
            // 返回文件名
            String resPath = Upload.UploadFile(filePath);
            filePath.delete();
            return R.ok().message("上传成功").data("resPath",resPath);
        }catch (IOException e){
            e.printStackTrace();
            return R.error().message("上传失败");
        }
    }


    @ApiOperation("文件删除")
    @PostMapping("/deleteUploadFile")
    public R  deleteUploadFile(@RequestBody WqunOOS wqunOOS) {
        boolean res = Upload.deleteUploadFIle(wqunOOS.getOosImgPath());
        if(res){
            return R.ok().message("文件删除成功");
        }else{
            return R.error().message("文件删除失败");
        }
    }


    @ApiOperation("项目上传")
    @RequestMapping("/uploadProject")
    @PreAuthorize("hasAnyAuthority('admin:util:uploadProject')")
    public R uploadProject(@RequestParam("uploadFile") MultipartFile mFile) {
        try {
            ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String folder =System.getProperty("user.dir") +  "/upload/img";
        File imgFolder = new File(folder);// 文件夹
        String fname = mFile.getOriginalFilename();// 获取文件名
        String ext = "." + fname.substring(fname.lastIndexOf(".")+1);// 获取文件后缀
        String dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());// 获取时间字符串
        String newFileName = dateTimeFormatter + UUID.randomUUID().toString().replaceAll("-","") + ext;// 生成新的文件名
        File filePath = new File(imgFolder, newFileName);// 文件在本机的全路径
        if (!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
        }
        try{
            mFile.transferTo(filePath);
            // 返回文件名
            String resPath = Upload.UploadProject(filePath);
            filePath.delete();
            return R.ok().message("项目上传成功").data("resPath",resPath);
        }catch (IOException e){
            e.printStackTrace();
            return R.error().message("项目上传失败");
        }
    }




}
